﻿using System.Collections;
using System.Collections.Generic;
using System;

public abstract class NetEvent {

    protected MessagePacket MMessagePacket;

    protected abstract ISerialize NewData();

    ISerialize m_Data;
    ISerialize data
    {
        get
        {
            if (m_Data == null)
                m_Data = NewData();

            return m_Data;
        }
    }

    private object m_Token;
    public object token => m_Token;

    public bool Init(object token,MessagePacket messagePacket)
    {
        m_Token = token;
        
        MMessagePacket = messagePacket;

        m_Data = NewData();

        return Read(MMessagePacket.data);
       
    }

    public bool Read(byte[] _data)
    {
        if (_data == null || _data.Length == 0)
        {
            GameDebug.LogShowy($"NetEvent _data  null");
            return true;
        }
        try
        {
            data.Read(_data);
        }
        catch (System.Exception e)
        {
            GameDebug.LogError(e);
            return false;
        }
        return true;
    }

    public void Excuate()
    {
        Excuate(data);
    }

    protected abstract void Excuate(ISerialize data);

}
